/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
/*
 * interface.c
 * Copyright (C) The Pennsylvania State University 2008 <tpk5010@psu.edu>
 * 
 * interface.c is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/* interface.c: where babies are made. And by babies, I mean our application's
   interface. Interface.c and interface.h define an AppUIData object which reads
   from the GladeXML file to create all the application's widgets. This object
   is what's thrown around in callback functions.
*/


#include <libintl.h>
#include "interface.h"
#define _(String) gettext(String)
#include <hildon-fm-2/hildon/hildon-file-chooser-dialog.h>
#include <hildon-1/hildon/hildon-note.h>
#include <glade/glade.h>

static void create_toolbar( AppUIData *main );

AppUIData* interface_main_view_new( AppData *data ) {

    /* Zero memory with g_new0 */
    AppUIData* result = g_new0( AppUIData, 1 );

    /* Initialize interface data */
	
    /* Store handle to app's data to view's data */
    result->data = data;
    /* Store handle to hildon_window to app's data */
    result->data->window = HILDON_WINDOW(hildon_window_new());
	
	/* Now for the incredibly tedious part of assigning widgets to GladeXML items */
	result->app_container = glade_xml_get_widget( result->data->gxml, "app_container" );
	result->toolbar = glade_xml_get_widget( result->data->gxml, "toolbar" );
	result->tb_addreport = glade_xml_get_widget( result->data->gxml, "tb_addreport" );
	result->tb_connectstatusicon = glade_xml_get_widget( result->data->gxml, "tb_connectstatusicon" );
	result->tb_gpsstatusicon = glade_xml_get_widget( result->data->gxml, "tb_gpsstatusicon" );
	result->tb_datetimebutton = glade_xml_get_widget( result->data->gxml, "tb_datetimebutton" );
	
	/* tab page 1 widgets */
	
	/* tab page 2 widgets */
	
	/* tab page 3 widget */
	result->latlabel = glade_xml_get_widget( result->data->gxml, "latlabel" );
	result->longlabel = glade_xml_get_widget( result->data->gxml, "longlabel" );
	result->altlabel = glade_xml_get_widget( result->data->gxml, "altlabel" );
	result->speedlabel = glade_xml_get_widget( result->data->gxml, "speedlabel" );
	result->typeoffixlabel = glade_xml_get_widget( result->data->gxml, "typeoffixlabel" );
	result->satsinuselabel = glade_xml_get_widget( result->data->gxml, "satsinuselabel" );
	
	/* tab page 4 widgets */
	
	/* Add the window container to the actual window */
	gtk_container_add( GTK_CONTAINER( result->data->window ), GTK_WIDGET ( result->app_container ) );
	create_toolbar ( result );
	
    /* Put misc GladeXML stuff here, like showing the widgets, or whatever
    // else needs initalized */

    return result;
}

/* Cleans up allocated memory */
void interface_main_view_destroy( AppUIData *main ) {

    g_free( main );
}

/* Show a nifty little error note */
/* A hildon note is basically a dialog box: should be used for
   critical, non-transient errors */
void interface_error( nc2iferror me, AppUIData *main ) {
    
	GtkWidget *dialog = NULL;
    gchar *msg = NULL;

	/*The reason this looks odd is internationalization */
	/*Strings are defined using gettext and are filled in at runtime */

    switch (me) {

    case NC2IF_ERROR_INVALID_URI:
        msg = g_strdup( _("nc2if_invalid_uri") );
        break;

    case NC2IF_ERROR_SAVE_FAILED:
        msg = g_strdup( _("nc2if_save_failed") );
        break;

    case NC2IF_ERROR_OPEN_FAILED:
        msg = g_strdup( _("nc2if_open_failed") );
        break;

    case NC2IF_NO_ERROR:
        return;

    default:
        g_assert_not_reached();
    }

    dialog = hildon_note_new_information( GTK_WINDOW( main->data->program ), msg );

    gtk_dialog_run( GTK_DIALOG( dialog ) );
    gtk_widget_destroy( dialog );
    g_free( msg );
}

/* Create toolbar to mainview */
static void create_toolbar ( AppUIData *main )
{
 	/* Set toolbar properties */
    gtk_toolbar_set_orientation( GTK_TOOLBAR(main->toolbar), GTK_ORIENTATION_HORIZONTAL );
    gtk_toolbar_set_style( GTK_TOOLBAR(main->toolbar), GTK_TOOLBAR_BOTH_HORIZ );

	/* These items are listed in interface.h */
	/* connect signals to buttons */

    /* Add toolbar to the HildonWindow */
    hildon_window_add_toolbar( main->data->window, GTK_TOOLBAR(main->toolbar) );
	
    /* Show toolbar */
	gtk_widget_show_all ( GTK_WIDGET(main->toolbar) );
    gtk_widget_show_all ( GTK_WIDGET(main->data->window) );

}
